#include <stdio.h>
#include <stdlib.h>

struct ListNode {
      int val;
      struct ListNode *next;
 };
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode *list = (struct ListNode *)malloc(sizeof(struct ListNode));
    list->val = 0;
    list->next = NULL;

    static int forward_last = 0;
    int count = 0;

    while((l1 != NULL) || (l2 != NULL) || (forward_last != 0)) {
        int temp1 = 0;
        int temp2 = 0;
        int sum = 0;
        int forward_now = 0;

        if(l1 != NULL) {
            temp1 = l1->val;
            l1 = l1->next;
            printf("l1 addr = %p, ", l1);
        }
        if(l2 != NULL) {
            temp2 = l2->val;
            l2 = l2->next;
            printf("l2 addr = %p, ", l2);
        }

        sum = temp1 + temp2;
        int value = (sum + forward_last) % 10;
        if((sum + forward_last) >= 10) {
            forward_now = (sum + forward_last) / 10;
        }

        if(count == 0) {
            list->val = value;
            list->next = NULL;

            printf("fisrt node insert-> %d ", list->val);
        } else {
            struct ListNode* p_list = NULL;
            p_list = list;
            while(p_list->next != NULL) {
                p_list = p_list->next;
            }
            struct ListNode *node = (struct ListNode *)malloc(sizeof(struct ListNode));

            node->val = value;
            node->next = NULL;
            p_list->next = node;

            printf("next node insert-> %d ", node->val);
        }
        printf("forward_last = %d\r\n", forward_last);
        forward_last = forward_now;
        count++;
    }

    return list;
}

int main(void)
{
    struct ListNode *l1 = (struct ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode *l1_node1 = (struct ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode *l1_node2 = (struct ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode *l1_node3 = (struct ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode *l1_node4 = (struct ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode *l1_node5 = (struct ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode *l1_node6 = (struct ListNode *)malloc(sizeof(struct ListNode));
    l1->val = 9;
    l1->next = l1_node1;

    l1_node1->val = 9;
    l1_node1->next = l1_node2;
    
    l1_node2->val = 9;
    l1_node2->next = l1_node3;

    l1_node3->val = 9;
    l1_node3->next = l1_node4;
    l1_node4->val = 9;
    l1_node4->next = l1_node5;
    l1_node5->val = 9;
    l1_node5->next = l1_node6;
    l1_node6->val = 9;
    l1_node6->next = NULL;

    struct ListNode *l2 = (struct ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode *l2_node1 = (struct ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode *l2_node2 = (struct ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode *l2_node3 = (struct ListNode *)malloc(sizeof(struct ListNode));
    l2->val = 9;
    l2->next = l2_node1;

    l2_node1->val = 9;
    l2_node1->next = l2_node2;
    
    l2_node2->val = 9;
    l2_node2->next = l2_node3;

    l2_node3->val = 9;
    l2_node3->next = NULL;

    printf(">>>>>>>> start \r\n");
    struct ListNode *result = addTwoNumbers(l1, l2);
    struct ListNode *p_result = NULL;
    p_result = result;
    while(p_result != NULL) {
        printf("%d ", p_result->val);
        p_result = p_result->next;
    }
    printf("\r\n>>>>>>>> stop \r\n");
    getchar();
    return 0;
}